package com.app.screencapture;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    /**
     * 百度AI工具类
     */
    private static SimpleDateFormat sdf = new SimpleDateFormat("HHmmssSSS", Locale.CHINA);
    private TextView textView;
    private String path;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tv_clcik);
        path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + getPackageNameWo(getApplicationContext()) + "/截屏";
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取当前屏幕的大小
                int width = getWindow().getDecorView().getRootView().getWidth();
                int height = getWindow().getDecorView().getRootView().getHeight();
                //生成相同大小的图片
                Bitmap temBitmap = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 );
                //找到当前页面的跟布局
                View view = getWindow().getDecorView().getRootView();
                //设置缓存
                view.setDrawingCacheEnabled(true);
                view.buildDrawingCache();
                //从缓存中获取当前屏幕的图片
                temBitmap = view.getDrawingCache();
                //保存图片
                if (temBitmap != null && width > 0 && height > 0) {

                    saveHeadPic(bitmap2StrByte(temBitmap), path);
                }
            }
        });
        PermissionUtil.requestPermission(this, new PermissionListener() {
            @Override
            public void onGranted() {
                createFileDir(path);
            }

            @Override
            public void onDenied(List<String> deniedPermissions) {

            }
        });
    }

    /**
     * 保存人头路径
     *
     * @param bitmapByte 人脸流
     */
    public String saveHeadPic(byte[] bitmapByte, String headDir) {
//    headDir=    FileUtils.ROOT_PATH+AppUtil.getPackageName(MyApplication.getContext())++ FileUtils.HEAD_PATH
        String filePath = headDir + File.separator + sdf.format(new Date()) + ".png";
        DebugLog.d(DebugLog.LOGHsc + " saveHeadPic--- filePath " + filePath);
        File filePng = new File(filePath);
        try {
            getFilesRead(filePng, bitmapByte);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath;
    }

    /**
     * Bitmap转换成Base64字符串
     *
     * @param bitmap
     * @return
     */
    public static byte[] bitmap2StrByte(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        return baos.toByteArray();
    }

    /**
     * 读流进去
     *
     * @param file  文件
     * @param mData
     * @throws IOException
     */
    public static void getFilesRead(File file, byte[] mData) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(mData, 0, mData.length));
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buff = new byte[8192];
        int len;
        while ((len = bis.read(buff)) != -1) {
            fos.write(buff, 0, len);
        }
        fos.flush();
        fos.close();
        bis.close();

    }

    /**
     * [获取应用程序版本名称信息]
     *
     * @param context
     * @return 当前应用的版本名称
     */
    public synchronized String getPackageNameWo(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.packageName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 创建文件夹
     *
     * @param fileDirName 根目录
     */
    public static void createFileDir(String fileDirName) {
        File fileDir = new File(fileDirName);
        boolean isRootSuccess = false;
        if (!fileDir.exists()) {
            isRootSuccess = fileDir.mkdirs();
            DebugLog.d("isRootSuccess:" + isRootSuccess);
        }
    }

}
